package com.springboot.controller;

import com.springboot.dto.DoctorChat;
import com.springboot.model.ChatRecord;
import com.springboot.model.UserLoginForm;
import com.springboot.service.DoctorChattingListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * 本控制台用于医生在线咨询
 */
@Controller
public class DoctorChattingController {
    @Autowired
    DoctorChattingListService doctorChattingListService;
    /**
     * 医生查看聊天内容
     */
    @CrossOrigin
    @RequestMapping(value = "/doctorGetMessageServe")
    public @ResponseBody
    String doctorChattingList(@RequestParam("tel") String doctorTel,
                              @RequestParam("otherTel") String otherTel) {
        System.out.println("医生查看聊天内容");
        try {
            UserLoginForm doctor=doctorChattingListService.findUserByTel(doctorTel);
            UserLoginForm other=doctorChattingListService.findUserByTel(otherTel);
            int doctorId=doctor.getUserId();
            int otherId=other.getUserId();
            ArrayList<DoctorChat> doctorChats=new ArrayList<>();
            ChatRecord chatRecord=new ChatRecord();
            chatRecord.setSenderId(doctorId);
            chatRecord.setReceiverId(otherId);
            SimpleDateFormat dateTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            ArrayList<ChatRecord> chatRecords=doctorChattingListService.findChatByRecord(chatRecord);
            System.out.println(chatRecords.size());
            for(int i=0;i<chatRecords.size();i++)
            {
                ChatRecord recordSender=chatRecords.get(i);
                System.out.println(recordSender.toString());
                if(recordSender.getAssistantId()==null)
                {
                    UserLoginForm receiver=doctorChattingListService.findUserById(otherId);
                    System.out.println(receiver.getUserName());
                    DoctorChat doctorChat=new DoctorChat();
                    doctorChat.setSelfName(doctor.getUserName());
                    doctorChat.setOtherName(receiver.getUserName());
                    doctorChat.setOtherLogoLink(receiver.getLogoLink());
                    doctorChat.setMessageTime(dateTime.format(recordSender.getChatTime()));
                    doctorChat.setIsRight("true");
                    doctorChat.setMessageContent(recordSender.getChatContent());
                    if(recordSender.getFlag2()==0)
                    {
                        doctorChat.setIsPicture("false");
                    }
                    else
                    {
                        doctorChat.setIsPicture("true");
                    }
                    doctorChat.setPhotoLink(recordSender.getPhotoLink());
                    System.out.println(doctorChat.toString());
                    doctorChats.add(doctorChat);
                }
                else
                {
                    UserLoginForm assistant=doctorChattingListService.findUserById(recordSender.getAssistantId());
                    DoctorChat doctorChat=new DoctorChat();
                    doctorChat.setSelfName(doctor.getUserName());
                    doctorChat.setOtherName(assistant.getUserName());
                    doctorChat.setOtherLogoLink(assistant.getLogoLink());
                    doctorChat.setMessageTime(dateTime.format(recordSender.getChatTime()));
                    doctorChat.setIsRight("false");
                    doctorChat.setMessageContent(recordSender.getChatContent());
                    if(recordSender.getFlag2()==0)
                    {
                        doctorChat.setIsPicture("false");
                    }
                    else
                    {
                        doctorChat.setIsPicture("true");
                    }
                    doctorChat.setPhotoLink(recordSender.getPhotoLink());
                    doctorChats.add(doctorChat);
                }
            }
            ChatRecord record=new ChatRecord();
            record.setSenderId(otherId);
            record.setReceiverId(doctorId);
            ArrayList<ChatRecord> chatRecordArrayList=doctorChattingListService.findChatByRecord(record);
            System.out.println(chatRecordArrayList.size());
            for(int i=0;i<chatRecordArrayList.size();i++)
            {
                ChatRecord recordReceiver=chatRecordArrayList.get(i);
                recordReceiver.setReceiverState("已读");
                doctorChattingListService.updateChatRecord(recordReceiver);
                UserLoginForm sender=doctorChattingListService.findUserById(otherId);
                DoctorChat doctorChat=new DoctorChat();
                doctorChat.setSelfName(doctor.getUserName());
                doctorChat.setOtherName(sender.getUserName());
                doctorChat.setOtherLogoLink(sender.getLogoLink());
                doctorChat.setIsRight("false");
                doctorChat.setMessageTime(dateTime.format(recordReceiver.getChatTime()));
                doctorChat.setMessageContent(recordReceiver.getChatContent());
                if(recordReceiver.getFlag2()==0)
                {
                    doctorChat.setIsPicture("false");
                }
                else
                {
                    doctorChat.setIsPicture("true");
                }
                doctorChat.setPhotoLink(recordReceiver.getPhotoLink());
                doctorChats.add(doctorChat);
            }
            //按消息时间排序
            Comparator<DoctorChat> comparator = new Comparator<DoctorChat>(){
                public int compare(DoctorChat s1, DoctorChat s2) {
                    //按照消息时间排序
                    return s1.messageTime.compareTo(s2.messageTime);
                }
            };
            //这里就会自动根据规则进行排序
            Collections.sort(doctorChats,comparator);
            return doctorChats.toString();
        } catch (Exception e) {
            return "False";
        }
    }
}
